package com.cskaoyan.user.service.impl;

import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.dto.UserVerifyRequest;
import com.cskaoyan.user.dto.UserVerifyResponse;
import com.cskaoyan.user.service.ICHYUserVerifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/17 16:28
 */
@Slf4j
@Service
public class ICHYUserVerifyServiceImpl implements ICHYUserVerifyService {

    @Autowired
    UserVerifyMapper userVerifyMapper;
    // @Autowired


    /**
     * @param request
     * @return
     */
    @Override
    public UserVerifyResponse userVerify(UserVerifyRequest request) {
        UserVerifyResponse userVerifyResponse = new UserVerifyResponse();
        /**
         * user/verify不知道是否需要`参数校验`
         *
         */
          request.requestCheck();

        Example example = new Example(UserVerify.class);
        // 给定条件mapper 查找数据库的条件;
        example.createCriteria().andEqualTo("uuid", request.getUuid()).andEqualTo("username", request.getUserName());

        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
        if (userVerifies==null) {
            userVerifyResponse.setCode("400");
            userVerifyResponse.setMsg("GET /user/verify 请求失败");
            return userVerifyResponse ;
        }else if(userVerifies!=null && userVerifies.size()==0){
            userVerifyResponse.setCode("401");
            userVerifyResponse.setMsg("GET /user/verify ; 该用户不存在");
            return userVerifyResponse ;
        }
        // 运行至此,必定存在该用户;
        userVerifyResponse.setMsg("GET /user/verify 请求成功");
        userVerifyResponse.setCode("200");

        /** 当注册后(数据库里设置为 is_verified 为 N), 会发送一个`激活`到邮箱,  随后若用户点击,则`激活成功`, 即is_verified 设置为 Y;
         * 2022年06月18日14:26:15 chy
         */
        Example exampleForIsVerified = new Example(UserVerify.class);
        Example.Criteria criteria = exampleForIsVerified.createCriteria();
        criteria.andEqualTo("uuid", request.getUuid()); // 以 uuid 作为 匹配用户的那条记录;功能类似于 id;
        /**
         * 将需要更新的参数封装到一个 UserVerify 对象里,即 entity;
         */
        UserVerify userVerify = new UserVerify();
        userVerify.setIsVerify("Y");
        // 执行更新; 参数 1 为一个 entity 对象,  参数 2 为条件实例;
        int i = userVerifyMapper.updateByExampleSelective(userVerify, exampleForIsVerified);

        return userVerifyResponse;
    }
}
